<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>员工列表</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        body a {
            text-decoration: none;
            color: #0d3a4b;
        }

        .active {
            color: #ffffff;
        }

        .wrap {
            width: 700px;
            height: 500px;
            background-color: skyblue;
            margin: 0 auto;
            position: relative;
        }

        .wrap #table {
            width: 460px;
            border-radius: 12px;
            position: absolute;
            left: 50%;
            top: 160px;
            transform: translateX(-50%);
            box-sizing: border-box;
            overflow: hidden;
        }

        .wrap #table thead {
            width: 100%;
            height: 30px;
            line-height: 30px;
            text-align: center;
            border-top-left-radius: 12px;
            border-top-right-radius: 12px;
            color: #fff;
            background: #4cbfe5;
        }

        .wrap #table thead a {
            color: #fff;
        }

        .wrap #table tbody {
            color: #19c2ff;
        }

        .wrap #table tbody a {
            color: #19c2ff;
        }

        .wrap #table tbody tr {
            background: #fff;
        }

        .ctrl {
            position: absolute;
            left: 200px;
            top: 100px;
        }

        .ctrl div {
            width: 300px;
            line-height: 30px;
            display: flex;
            flex-direction: row;
            justify-content: space-around;
        }

        h1 {
            text-align: center;
            padding-top: 20px;
            color: white;
        }
    </style>
</head>

<body>
    <div class="wrap">
        <h1>员工列表</h1>
        <div class="ctrl">
            <div class="age_sort nu">
                <a href="javascript:;" class="bt1">年龄从小到大</a>
                <a href="javascript:;" class="bt2">年龄从大到小</a>
                <a href="javascript:;" class="active">默认排序</a>
            </div>
            <div class="gender_show">
                <a href="javascript:;" class="bt3">只显示男性</a>
                <a href="javascript:;" class="bt4">只显示女性</a>
                <a href="javascript:;" class="active">默认</a>
            </div>
        </div>
        <table id="table">
            <thead>
                <tr>
                    <th>id</th>
                    <th>姓名</th>
                    <th>年龄</th>
                    <th>性别</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <th>0</th>
                    <th>小明</th>
                    <th>24</th>
                    <th>男</th>
                </tr>
            </tbody>
        </table>
    </div>
    <script>
        var data = [
            {
                id: 1,
                name: '小明',
                age: 24,
                gender: '男'
            },
            {
                id: 2,
                name: '小芳',
                age: 30,
                gender: '女'
            },
            {
                id: 3,
                name: '小美',
                age: 31,
                gender: '女'
            },
            {
                id: 4,
                name: '小刚',
                age: 21,
                gender: '男'
            },
            {
                id: 5,
                name: '小琪',
                age: 18,
                gender: '女'
            }
        ];
        // 深拷贝函数
            function deepCopy(obj){
                var obj1 = Object.prototype.toString.call(obj) === '[object Array]' ? [] : {};
                obj.forEach((itme,index) => {
                    if (typeof itme === 'Object'){
                        obj1[index] = deepCopy(obj[index])
                    }else{
                        obj1[index] = obj[index]
                    }
                })
                return obj1;
            }
        // 数据渲染视图
        randerDom(data);
        function randerDom(data){
            // 获取元素清空里面的内容
            let tbodyEl = document.querySelector('tbody');
            tbodyEl.innerHTML = '';
            // 根据数据循环创建渲染视图
            data.forEach((itme,index) => {
                let trEL = document.createElement('tr');
                trEL.innerHTML = `
                    <th>${itme.id}</th>
                    <th>${itme.name}</th>
                    <th>${itme.age}</th>
                    <th>${itme.gender}</th>
                `
                tbodyEl.appendChild(trEL)
            })
        }
        
        // 设置默认的年龄和性别排序下标
        let ageIdx = 2;
        let genderIdx = 2;
        // 创建年龄排序数据：
        let ageArr = [data => deepCopy(data).sort((a,b) => {return a.age - b.age}),
            data =>deepCopy(data).sort((a,b) => b.age - a.age),
            data =>deepCopy(data)
        ];
        // 创建性别排序；
        let genderArr = [data => deepCopy(data).filter(itme => itme.gender === '男'),
            data => deepCopy(data).filter(itme => itme.gender === '女'),
            data => deepCopy(data)
        ]
        console.log(ageArr);
        console.log(genderArr);

        // 获取年龄排序按钮设置点击事件渲染视图
        let ageBtn = document.querySelectorAll('.age_sort a')
        ageBtn.forEach(function(itme,index){
            itme.onclick = function(){
                ageBtn.forEach(function(itme){
                    itme.classList.remove('active');
                })
                this.classList.add('active');
                ageIdx = index;

                // 调用数据渲染视图
                console.log(genderArr[index]);
                let num = genderArr[index](data);
                let newdata = ageArr[index](num);
                randerDom(newdata);
            }
        })
         // 获取性别排序按钮设置点击事件渲染视图
        let genderBtn =  document.querySelectorAll('.gender_show a')
        genderBtn.forEach(function(itme,index){
            itme.onclick = function(){
                genderBtn.forEach(function(itme){
                    itme.classList.remove('active');
                })
                this.classList.add('active');
                genderIdx = index;

                // 调用数据渲染视图
                let num = ageArr[index](data);
                let newdata = genderArr[index](num);
                randerDom(newdata);
            }
        })
            
    </script>
</body>

</html>